package com.hivekion.system.controller;

import com.hivekion.common.annotation.AutoLog;
import com.hivekion.common.enums.OperationTypeEnum;
import com.hivekion.common.exception.BusinessException;
import com.hivekion.system.domain.SysStatisticsBiName;
import com.hivekion.system.domain.vo.SysStatisticsBiNameCreateInputVo;
import com.hivekion.system.domain.vo.SysStatisticsBiNameInputVo;
import com.hivekion.system.domain.vo.SysStatisticsBiNameViewVo;
import com.hivekion.system.service.ISysStatisticsBiNameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/statisticsBiName")
public class SysStatisticsBiNameController {
    @Autowired
    ISysStatisticsBiNameService statisticsBiNameService;

    @GetMapping("/getList")
    @AutoLog(value = "根据大分类获取所有报表类型", operationType = OperationTypeEnum.SELECT, module = "系统管理/报表类型管理")
    public List<SysStatisticsBiNameViewVo> getList() {
        return statisticsBiNameService.getList();
    }

    @PostMapping("/add")
    @PreAuthorize("@Permission.hasPermi('bi_name:add')")
    @AutoLog(value = "新增报表类型", operationType = OperationTypeEnum.INSERT, module = "系统管理/报表类型管理")
    public String add(@RequestBody SysStatisticsBiNameCreateInputVo inputVo) {
        if (statisticsBiNameService.checkBiNameNameUnique(inputVo.getName(), "")) {
            throw new BusinessException(500, "报表名称重复");
        }
        return statisticsBiNameService.create(inputVo);
    }

    @GetMapping("/getInfo")
    @AutoLog(value = "获取单个报表类型系", operationType = OperationTypeEnum.SELECT, module = "系统管理/报表类型管理")
    public SysStatisticsBiName getInfo(String nameId) {
        return statisticsBiNameService.getInfo(nameId);
    }

    @PutMapping("/edit")
    @PreAuthorize("@Permission.hasPermi('bi_name:edit')")
    @AutoLog(value = "更改报表类型", operationType = OperationTypeEnum.UPDATE, module = "系统管理/报表类型管理")
    public boolean update(@RequestBody SysStatisticsBiNameInputVo inputVo) {
        return statisticsBiNameService.update(inputVo);
    }

    @PostMapping("/checkNameUnique")
    public boolean checkNameUnique(String itemName, String itemId) {
        return statisticsBiNameService.checkBiNameNameUnique(itemName, itemId);
    }

    @DeleteMapping("/remove")
    @PreAuthorize("@Permission.hasPermi('bi_name:delete')")
    @AutoLog(value = "删除报表类型", operationType = OperationTypeEnum.DELETE, module = "系统管理/报表类型管理")
    public boolean delete(String binameid) {
        // TODO 需要删除 统计项和子项的所有数据
        return statisticsBiNameService.delete(binameid);
    }

    @PutMapping("/updateIntegral")
    @PreAuthorize("@Permission.hasPermi('bi_name:edit')")
    @AutoLog(value = "更新积分规则", operationType = OperationTypeEnum.UPDATE, module = "系统管理/报表类型管理")
    public boolean updateIntegral(String typeId, int integral) {
        SysStatisticsBiName byId = statisticsBiNameService.getById(typeId);
        if (byId == null) {
            throw new BusinessException(500, "未找到统计表单信息！");
        }
        byId.setIntegralRule(integral);
        return statisticsBiNameService.updateById(byId);
    }
}
